const funApi = require('./router/bds/funcApi.js');
const Response = require("./class/Response.js");
// 注册PAPI

function registerPAPI(){
    PAPI.registerServerPlaceholder(funApi.getOnlinePlayersNum, 'CoordinationWss', 'onlinePlayersNum'); // 协调服务端在线玩家人数
    PAPI.registerServerPlaceholder(funApi.getOnlinePlayersName, 'CoordinationWss', `onlinePlayersName`); // 协调服务端在线玩家列表
}

//================================
// 直接导出的API

/**
 * @description: 向服务端发送json消息
 * @param {object} json
 * @return {*}
 */
function sendJson(json){
    return WSC.client?.sendJson(json);
}
ll.exports(sendJson, 'CoordinationWss', 'sendJson');

/**
 * @description: 向服务端发送数据
 * @param {object} json 数据
 * @return {*}
 */
function sendData(json){
    let res = new Response();
    res.header.url = "/llapi/data";
    res.body.data = json;
    WSC.client.sendJson(res.json);
}
ll.exports(sendData, 'CoordinationWss', 'sendData');

/**
 * @description: 向所有BDS广播数据
 * @param {object} json 数据
 * @return {*}
 */
function broadcastData(json){
    let res = new Response();
    res.header.source_bds_uuid = CONF.CONFIG.get('Websocket')['BDSUuid'];
    res.header.source_bds_serverName = CONF.CONFIG.get('Websocket')['ServerName'];
    res.header.url = "/llapi/data/broadcast";
    res.body.data = json;
    WSC.client.sendJson(res.json);
}
ll.exports(broadcastData, 'CoordinationWss', 'broadcastData');

/**
 * @description: 发送WSS服务端控制台消息
 * @param {string} msg 消息
 * @param {string} level 日志等级
 * @return {*}
 */    
function sendServerLogger(msg, level='info'){
    return WSC.client.sendServerLogger(msg, level);
}
ll.exports(sendServerLogger, 'CoordinationWss', 'sendServerLogger');


// 注册data事件
WSC.addApiEventName('data'); 
WSC.get('/llapi/data', (wsclient, req, res)=>{
    WSC.api.listener.emit('data', res.json);
});

//================================
// 外部监听器实现, 发送到外部回调
for (let eventName in WSC.api.remoteCallFunctions){
    WSC.api.listener.on(eventName, (...args)=>{
        for (let namespace in WSC.api.remoteCallFunctions[eventName]){
            for (let name in WSC.api.remoteCallFunctions[eventName][namespace]){
                WSC.api.remoteCallFunctions[eventName][namespace][name](...args);
            }
        }
    });
}

/**
 * @description: 注册外部监听器回调
 * @param {string} eventName 监听的事件名
 * @param {string} namespace 导出函数的命名空间, 一般设为插件名
 * @param {string} name 导出函数的名称
 * @return {boolean} 监听器回调是否注册成功
 */
function registerListenerCall(eventName, namespace, name){
    // 无此api返回false
    if (!(eventName in WSC.api.remoteCallFunctions)){
        return false;
    }
    // 导入外部回调
    if (WSC.api.remoteCallFunctions[eventName][namespace] == undefined){
        WSC.api.remoteCallFunctions[eventName][namespace] = {[name]: ll.import(namespace, name)};
    }else{
        WSC.api.remoteCallFunctions[eventName][namespace][name] = ll.import(namespace, name);
    }
    return true;
}

ll.exports(registerListenerCall, 'CoordinationWss', 'registerListenerCall');


module.exports = {
    registerPAPI
};


